package server

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"onvif_server/api"
	"onvif_server/internal/handler"
	"onvif_server/internal/middleware"
	"onvif_server/old_api"
	"onvif_server/pkg/cfgstruct"
	"onvif_server/pkg/jwt"
	"onvif_server/pkg/server/http"
	"onvif_server/pkg/version"
	"onvif_server/pkg/ws"
)

func NewHTTPServer(
	log *zap.Logger,
	jwt *jwt.JWT,
	conf *http.Config,
	assetsHandler *handler.AssetsHandler,
	userHandler *handler.UserHandler,
	cameraHandler *handler.CameraHandler,
	settingHandler *handler.SettingHandler,
	subHandler *handler.SubscriptionHandler,
	alarmHandler *handler.AlarmHandler,
	systemHandler *handler.SystemHandler,
	oldApiHandler *old_api.OldApiHandler,
	wsManage *ws.Manage,
) *http.Server {
	switch cfgstruct.DefaultsType() {
	case cfgstruct.DefaultsRelease:
		gin.SetMode(gin.ReleaseMode)
	case cfgstruct.DefaultsTest:
		gin.SetMode(gin.TestMode)
	default:
		gin.SetMode(gin.DebugMode)
	}
	gEngine := gin.Default()
	//gEngine := gin.New()
	gEngine.Use(middleware.CORSMiddleware())
	s := http.NewServer(
		gEngine,
		log,
		conf,
	)
	//注册静态路由
	assetsHandler.Register(s.Engine)

	//兼容旧的外部接口
	oldApiHandler.Register(s.Engine)

	//读取上传文件
	gEngine.GET("/upload/*file", systemHandler.GetUploadFile)
	//subscription 测试地址
	gEngine.POST("/subscribe/report", systemHandler.SubscribeReport)

	apiGroup := s.Group("/api",
		middleware.CORSMiddleware(),
		middleware.RequestLogMiddleware(log.Named("middleware")),
	)
	// No route group has permission
	apiGroup.POST("/login", userHandler.Login)
	apiGroup.POST("/refresh_token", userHandler.RefreshToken)
	//版本信息
	apiGroup.GET("/version", func(ctx *gin.Context) {
		api.Success(ctx, version.Build)
	})

	//需要登陆
	authRouter := apiGroup.Group("", middleware.Auth(jwt, log))
	{
		// No route group has permission
		authRouter.POST("/logout", userHandler.Logout)
		authRouter.GET("/profile", userHandler.Profile)
		//上传文件
		authRouter.POST("/upload", systemHandler.Upload)
		//camera 摄像头相关
		{
			authRouter.GET("/camera", cameraHandler.List)
			authRouter.POST("/camera", cameraHandler.Create)
			authRouter.PUT("/camera", cameraHandler.Update)
			authRouter.DELETE("/camera", cameraHandler.Delete)
			authRouter.GET("/camera/discovery", cameraHandler.Discovery)               //扫描摄像头
			authRouter.POST("/camera/check_connection", cameraHandler.CheckConnection) //检查摄像头是否在线

			authRouter.PUT("/camera/sync_datetime", cameraHandler.SyncDatetime) //同步摄像头时间
			authRouter.GET("/camera/datetime", cameraHandler.GetDatetime)       //获取摄像头时间
			authRouter.POST("/camera/snap_url", cameraHandler.GetSnapUrl)       //获取截图地址

			authRouter.GET("/camera/hour_video", cameraHandler.GetM3u8Hour) // 获取m3u8文件
			authRouter.GET("/camera/day_video", cameraHandler.GetM3u8Day)   // 获取m3u8文件

			authRouter.GET("/camera/get_mp4_by_img", cameraHandler.GetMp4ByImage) // 通过名称获取该时间段的视频
			authRouter.GET("/camera/history", cameraHandler.GetHistory)           // 返回设摄像头历史文件记录列表
			authRouter.GET("/camera/video_search", cameraHandler.VideoSearch)     // 搜索视频

		}

		//subscription 订阅
		{
			authRouter.GET("/subscription", subHandler.List)
			authRouter.POST("/subscription", subHandler.Create)
			authRouter.PUT("/subscription", subHandler.Update)
			authRouter.DELETE("/subscription", subHandler.Delete)
			authRouter.GET("/subscription/topics", subHandler.Topics) //订阅主题列表
			authRouter.POST("/subscription/test", subHandler.Test)    //测试推送消息
		}

		//setting 系统配置
		{

			//network
			authRouter.GET("/setting/network", settingHandler.GetNetwork)
			authRouter.POST("/setting/network", settingHandler.SetNetwork)
			authRouter.GET("/setting/storage", settingHandler.GetStorage)
			authRouter.POST("/setting/storage", settingHandler.SetStorage)
			authRouter.GET("/setting/storage/drivers", settingHandler.StorageDrivers)
			authRouter.GET("/box", settingHandler.GetBox)
			authRouter.POST("/box", settingHandler.SetBox)

		}

		//alarm 告警
		{
			authRouter.GET("/alarm", alarmHandler.List)
			authRouter.POST("/alarm", alarmHandler.Create)
			authRouter.POST("/alarm_raw", alarmHandler.RawCreate)
			authRouter.DELETE("/alarm", alarmHandler.Delete)
			authRouter.GET("/alarm/detail", alarmHandler.Deatil)
			authRouter.POST("/alarm/setting", alarmHandler.SetSetting)
			authRouter.GET("/alarm/setting", alarmHandler.GetSetting)
			authRouter.POST("/alarm/process", alarmHandler.Process) //告警处理
		}

		//system 系统操作
		{
			authRouter.GET("/system", systemHandler.Info)         //系统信息
			authRouter.POST("/reboot", systemHandler.Reboot)      //重启
			authRouter.POST("/upgrade", systemHandler.Upgrade)    //升级
			authRouter.GET("/discovery", systemHandler.Discovery) //升级
			authRouter.GET("/ws", systemHandler.Ws)               //升级
		}
	}

	//ws的路由注册
	wsManage.Route(api.WsActionAlarmPopup, alarmHandler.WsAlarmPopup)           //弹窗消息
	wsManage.Route(api.WsActionAlarmPopupClose, alarmHandler.WsAlarmPopupClose) //关闭弹窗消息
	wsManage.Route(api.WsActionAlarmNotify, alarmHandler.WsAlarmNotify)         //关闭弹窗消息
	return s
}
